package com.cqc.manage.controller;

import com.cqc.manage.dto.BusinessDto;
import com.cqc.manage.dto.EngineerBussinessDto;
import com.cqc.manage.dto.JsonResultDto;
import com.cqc.manage.dto.PayloadDto;
import com.cqc.manage.entity.MessageBusiness;
import com.cqc.manage.entity.User;
import com.cqc.manage.service.Imp.business.BusinessChargeNoticeService;
import com.cqc.manage.service.Imp.business.BusinessService;
import com.cqc.manage.token.JwtTokenService;
import com.nimbusds.jose.JOSEException;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

/**
 * 业务收费通知controller
 */

@RestController
@RequestMapping("/chargetNotice")
@Api(tags = "chargetNotice" , description = "收费通知")
public class BusinessChargetNoticeController {


    private Logger LOG = LoggerFactory.getLogger(BusinessChargetNoticeController.class);

    @Autowired
    private BusinessChargeNoticeService businessChargeNoticeService;

    @Autowired
    private JwtTokenService jwtTokenService;

    @Autowired
    private BusinessService businessService;

    /**
     * 获取收费通知pdf的图片
     * @param contractNo
     * @param cost
     * @return
     */
    @RequestMapping(value = "/getChartgetNotice", method = RequestMethod.GET ,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getChargetNotice(@RequestParam("contractNo")String contractNo,@RequestParam("cost")String cost){

        String busChaNotPath = businessChargeNoticeService.getBusChaNot(contractNo, cost);
        if (busChaNotPath != null) {
//            String s = BusinessChargeNoticeService.ImageToBase64ByLocal(busChaNotPath);
            return new JsonResultDto<String>("收费通知生成成功","200",busChaNotPath);
        }
        return new JsonResultDto("204","收费通知生成失败");
    }


    /**
     * 获取未发送收费通知的业务
     * @param engineerBussinessDto
     * @return
     */
    @RequestMapping(value = "/getNoChagetNotice", method = RequestMethod.POST , produces = MediaType.APPLICATION_JSON_VALUE)
    public EngineerBussinessDto getNoChagetNotice(@RequestBody EngineerBussinessDto engineerBussinessDto, HttpServletRequest request) throws ParseException, JOSEException {
        String authHeader = request.getHeader("Authorization");
        String token = authHeader.substring(7);
        // 验证token
        PayloadDto test = jwtTokenService.verifyTokenByHMAC(token);
        LOG.info("用户账户:{}",test.getUserName());
        engineerBussinessDto.setEngLimsAccount(test.getUserName());
        businessChargeNoticeService.getChargeNoticeBusiness(engineerBussinessDto);
        return engineerBussinessDto;
    }

    /**
     * 收费通知------搜索单个业务
     * @param contractNo
     * @return
     */
    @RequestMapping(value = "/getOneBusinessOnChagetNotice", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getOneMessageBusiness(@RequestParam("contractNo") String contractNo){
        LOG.info("收费通知，单个业务查询:{}",contractNo);
        BusinessDto business = businessService.getBusiness(contractNo);
        if (business == null){
            return new JsonResultDto("error","201",business);
        }
        LOG.info("收费通知，单个业务查询结果:{}",business);
        return new JsonResultDto("success","200",business);
    }


    /**
     * 多个因为业务的收费通知
     * @param businessList
     * @return
     */
    @RequestMapping(value = "/getMultipleCheckNoice" , method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public JsonResultDto getMultipleCheckNoice(@RequestBody List<BusinessDto> businessList){
        LOG.info("多个业务:{}",businessList);
        String multipleBusChaNotPath = businessChargeNoticeService.getMultipleBusChaNot(businessList);

        LOG.info("收费通知:{}",multipleBusChaNotPath);
        if (multipleBusChaNotPath != null) {
//            String s = BusinessChargeNoticeService.ImageToBase64ByLocal(busChaNotPath);
            return new JsonResultDto<String>("收费通知生成成功","200",multipleBusChaNotPath);
        }
        return new JsonResultDto("201","error");
    }




}
